<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Payroll extends User_Controller
{
	private $response = array();
	private $userModel;
	private $clientModel;
    public function __construct()
    {
        parent::__construct();
        $this->userModel = $this->load->model('User_Model');
        $this->clientModel = $this->load->model('Client_Model');
        $this->payrollModel = $this->load->model('Payroll_Model');
        $this->timesheetModel = $this->load->model('Timesheet_Model');
        $this->load->model('Holiday_Model');
        $this->load->helper('RequestManager');
        $this->requestManager = RequestManager::getInstance();
        $this->settings = $this->config->item('settings');
        
    }

	public function get_payroll($api,$payroll_period_id){
		$data = array();
		$result = $this->payrollModel->getAllByPeriod($api, $payroll_period_id);
		if(isset($result)){
			$response["message"] = "success";
			
			if(!empty($result)){
				foreach($result as $pay){
					$earnings = $this->payrollModel->compute($pay);
					$pay->earnings = $earnings;
				}
			}
			$response["data"] = $result;
			$response["pending"] = $this->payrollModel->getCountPayrollStatus($api, $payroll_period_id, 'pending');
			$response["confirmed"] = $this->payrollModel->getCountPayrollStatus($api, $payroll_period_id, 'confirmed');
			$response["submitted"] = $this->payrollModel->getCountPayrollStatus($api, $payroll_period_id, 'submitted');
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('payroll', $data);
	}
	
	
	public function view_user($api,$payroll_period_id,$user_id){
		$data = array();
		
		// lookup payroll available
		$user_payroll = $this->payrollModel->userPayroll($api, $payroll_period_id, $user_id);
		$member_info = $this->userModel->getUserProfile($api, $user_id);
		$payroll = $this->payrollModel->getPayrollPeriod($api,$payroll_period_id);
		$user_contributions = $this->userModel->getContributions($api,$user_id);
		//$absences = $this->timesheetModel->getNumberOfAbsences($api, $payroll_period_id, $user_id);
		//var_dump($user_contributions);

		if((count($user_payroll) == 0)){
			$result = $this->timesheetModel->getSummaryLogsByUser($api, $payroll_period_id, $user_id);
			//var_dump($result);
		//	$absences = $this->timesheetModel->getNumberOfAbsences($api, $payroll_period_id, $user_id);
			
			//$member_info = $this->userModel->getUserProfile($api, $user_id);
			$special_holiday = $this->timesheetModel->getHolidayByType($api, $payroll_period_id, $user_id,'special');
			$regular_holiday = $this->timesheetModel->getHolidayByType($api, $payroll_period_id, $user_id,'regular');
			if(!empty($regular_holiday)){
				$regular_worked_holidays = $this->timesheetModel->getMemberWorkedHolidays($regular_holiday, $user_id);
			}
			
			if(!empty($special_holiday)){
				$special_worked_holidays = $this->timesheetModel->getMemberWorkedHolidays($special_holiday, $user_id);
			}
	
			
			if($result != 0 ){
				//$response["message"] = "success";
				$response["data"] = $result[0];
				
				$response["regular_holiday"] = isset($regular_worked_holidays)?$regular_worked_holidays:0;
				$response["special_holiday"] = isset($special_worked_holidays)?$special_worked_holidays:0;
				$response["rest_day"] = $this->timesheetModel->getUserRestDay($api, $payroll_period_id, $user_id);
				//$response["rest_day"] 
				// save data to payroll
				$inserted_id = $this->payrollModel->save($response,$payroll_period_id,$user_id);
				$response["user_payroll_id"] = $inserted_id;
				
				// call again
				if($inserted_id > 0 ){
					$response = array();
					$user_payroll = $this->payrollModel->userPayroll($api, $payroll_period_id, $user_id);
					$response['user_payroll'] = $user_payroll[0];
				}
				
			}
		}else {
			//$response['message'] = "success";
			$response['user_payroll'] = $user_payroll[0];
		}
		
		$response['message'] = "success";
		$response["member"] = $member_info[0];
		$response["period"] = $payroll;
		$response["contributions"] = $user_contributions;
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('payroll', $data);
	}
	
	public function client_payroll_periods($api){
		$data = array();
		$result = $this->payrollModel->getClientPayrollPeriods($api);
		if(isset($result) && is_array($result)){
			$response["message"] = "success";
			$response["data"] = $result;
			$data["response"] = ObjectUtils::arrayToJSON($response);
		}
		$this->load->view('payroll', $data);
	}
	
	public function update_field($api){
		$data = array();
		$response["message"] = "success";
		$params = array("{$_POST['field_id']}"=>$_POST['field_value']);
		
		if(is_numeric($_POST['field_value'])){
			$this->payrollModel->updateField($api,$params, $_POST['payroll_period_id'], $_POST['user_id'], $_POST['pid']);
		}
		
		$response["field_value"] = floatval($_POST['field_value']);
		//$response["data"] = $params;
		$response["message"] = "success";
		$data["response"] = ObjectUtils::arrayToJSON($response);
		$this->load->view('payroll', $data);
	}
	
	public function save_payroll_amount($api){
		if($this->input->post()){
			$params = $this->input->post();
			
			if($params['status'] == 'confirmed' || $params['status'] == 'submitted') {
				// update payroll table
				
				$prm['status'] = $params['status'];
				$prm['hdmf'] = $params['hdmf'];
				$prm['philhealth'] = $params['philhealth'];
				$prm['sss_loan'] = $params['sss_loan'];
				$prm['sss'] = $params['sss'];
				//$prm['other_deductions'] = $params['other_deductions'];
				$prm['hdmf_loan'] = $params['hdmf_loan'];
				$prm['tax'] = $params['tax'];
				$prm['user_id'] = $params['user_id'];
				$prm['net_pay'] = $params['net_pay'];
				$prm['gross_salary'] = $params['gross_salary'];
				$return = $this->payrollModel->saveOrUpdatePayroll($api, $prm, $params['pid']);
			} 
			// unset canteen, uniform
			unset($params['canteen']);
			unset($params['uniform']);
			if($params['status'] == 'submitted') {
				unset($params['status']);
				unset($params['user_id']);
				unset($params['payroll_period_id']);
				$return = $this->payrollModel->saveOrUpdatePayrollAmount($api, $params);
			}
			
			$response["message"] = "success";
			//$response["data"] = $params;
			$data["response"] = ObjectUtils::arrayToJSON($response);
			$this->load->view('payroll', $data);
		}
	}
	
}